package cn.jh.common.proxy;

import cn.jh.common.pay.client.FreeQuickClient;
import cn.jh.common.pay.client.FreeQuickRequest;
import cn.jh.common.pay.client.FreeQuickResponse;
import cn.jh.common.pay.enumtype.EncryptTypeEnum;
import cn.jh.common.pay.enumtype.FreeQuickStateEnum;
import cn.jh.common.utils.CommonConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Map;

public class FreeQuickProxyService {

    private static final Logger log = LoggerFactory.getLogger(FreeQuickProxyService.class);

    private static final String AGENT_NO = CommonConstants.AGENT_NO;
    private static final String AGENT_KEY = CommonConstants.AGENT_KEY;

    private static final String BASE_URL = CommonConstants.BASEURL;

    // 商户查询费率信息
    private static final String QUERY_MERCHANT_FEE_INFO = "/rest/v1.0/paybar/queryMerchantFeeInfo";
    // 商户修改结算卡信息
    private static final String MODIFY_MERCHANT_SETTLE_INFO = "/rest/v1.0/paybar/modifyMerchantSettlementInfo";
    // 商户查询已绑卡
    private static final String QUERY_BANK_CARD_LIST = "/rest/v1.0/paybar/queryBankCardList";
    // 商户余额查询
    private static final String MERCHANT_WALLET = "/rest/v1.0/paybar/queryMerchantWallet";
    // 商户修改产品费率
    private static final String MODIFY_MERCHANT_FEE_INFO = "/rest/v1.0/paybar/modifyMerchantFeeInfo";
    // 商户开通产品
    private static final String REGIST_MERCHANT_PRODUCT = "/rest/v1.0/paybar/registMerchantProduct";

    private static final String QUERY_MERCHANT_INFO = "/rest/v1.0/paybar/queryMerchantInfo";
    // 商户查询提交结果
    public static final String QUERY_WITHdRAW = "/rest/v1.0/paybar/queryWithdraw";

    /**
     * 公共方法：封装FreeQuickRequest请求头
     * @return
     */
    private static FreeQuickRequest freeQuickRequest() {
        FreeQuickRequest requestBody = new FreeQuickRequest(AGENT_NO, AGENT_KEY);
        requestBody.setSupportSign(false);
        requestBody.setEncryptType(EncryptTypeEnum.AES);
        return requestBody;
    }

    /**
     * 公共方法：创建freeQuickRequest请求并处理返回数据
     * @param requestUrl SDK地址
     * @param requestBody 请求体
     * @return
     */
    private static Map<String, String> freeQuickResponse(String requestUrl, FreeQuickRequest requestBody) {
        FreeQuickResponse response = FreeQuickClient.post(requestUrl, requestBody);
        FreeQuickStateEnum state = response.getState();
        if (CommonConstants.BASE_SUCCESS.equals(state.name())) {
            // 业务请求成功
            return response.getResultMap();
        } else {
            return null;
        }
    }

    /**
     * 商户查询费率信息：代理商通过该接口查询商户所有已开通产品的费率信息。
     * @param merchantNo 商户编号
     * @return Map<String, String>
     */
    public static Map<String, String> queryMerchantFeeInfo(String merchantNo) {
        String requestUrl = BASE_URL + QUERY_MERCHANT_FEE_INFO;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("merchantNo", merchantNo);

        return freeQuickResponse(requestUrl, requestBody);
    }

    /**
     * 商户修改结算卡信息接口：代理商通过该接口可修改已绑定的结算卡信息。
     * @param merchantNo                商户编号
     * @param bindMobile                绑定手机号码
     * @param settleBankAccountNo       结算银行卡号
     * @param settleBankAccountName     结算卡银行开户名
     * @param settleBankName            结算卡银行开户支行名
     * @param settleBankSubName         结算卡开户行联行号
     * @param settleBankAbbr            结算卡银行缩写
     * @param settleBankChannelNo       结算卡开户行联行号
     * @param settleBankCardProvince    结算卡开户省
     * @param settleBankCardCity        结算卡开户市
     * @return Map<String, String>
     */
    public static Map<String, String> modifyMerchantSettlementInfo(String merchantNo, String bindMobile, String settleBankAccountNo, String settleBankAccountName,
                                                                   String settleBankName, String settleBankSubName, String settleBankAbbr, String settleBankChannelNo,
                                                                   String settleBankCardProvince, String settleBankCardCity) {
        String requestUrl = BASE_URL + MODIFY_MERCHANT_SETTLE_INFO;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("merchantNo", merchantNo);
        requestBody.addParam("bindMobile", bindMobile);
        requestBody.addParam("settleBankAccountNo", settleBankAccountNo);
        requestBody.addParam("settleBankAccountName", settleBankAccountName);
        requestBody.addParam("settleBankName", settleBankName);
        requestBody.addParam("settleBankSubName", settleBankSubName);
        requestBody.addParam("settleBankAbbr", settleBankAbbr);
        requestBody.addParam("settleBankChannelNo", settleBankChannelNo);
        requestBody.addParam("settleBankCardProvince", settleBankCardProvince);
        requestBody.addParam("settleBankCardCity", settleBankCardCity);

        return freeQuickResponse(requestUrl, requestBody);
    }

    /**
     * 商户查询已绑卡接口：商户通过该接口可以查询已绑定的所有银行卡信息。
     * @param merchantNo 商户号
     * @param channelProductCode：非必传，不传查该商户所有通道成功的绑卡记录，有值查该通道下面的成功绑卡记录。
     * @return
     */
    public static Map<String, String> queryBankCardList(String merchantNo, String channelProductCode) {
        String requestUrl = BASE_URL + QUERY_BANK_CARD_LIST;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("merchantNo", merchantNo);
        requestBody.addParam("channelProductCode", channelProductCode);

        return freeQuickResponse(requestUrl, requestBody);
    }

    /**
     * 商户提现余额查询接口：商户通过该接口可以从平台查询用户的余额结果，根据余额判断是否进行提现。
     * @param merchantNo 商户号
     * @return
     */
    public static Map<String, String> queryMerchantWallet(String merchantNo) {
        String requestUrl = BASE_URL + MERCHANT_WALLET;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("merchantNo", merchantNo);

        return freeQuickResponse(requestUrl, requestBody);
    }

    /**
     * 商户修改产品费率：代理商通过该接口可修改已开通的产品费率信息
     * @param merchantNo 商户编号
     * @param channelProductCode 通道产品编码
     * @param cardType 卡类型：借记卡-DEBIT，贷记卡-CREDIT
     * @param bizType 费类型：交易-TRADE，提现费率-WITHDRAW_RATE，单笔提现费-WITHDRAW_SIGLE
     * @param feeValue 费率：BigDecimal(10, 4)，精度为小数位后4位；注：当修改提现手续费时精度为小数位后2位，不能大于等于0.05
     * @param capAmount 封顶金额：BigDecimal(10, 2)，封顶金额，精度为小数位后2位,不填默认不修改
     * @return
     */
    public static Map<String, String> modifyMerchantFeeInfo(String merchantNo, String channelProductCode, String cardType, String bizType, String feeValue, String capAmount) {
        String requestUrl = BASE_URL + MODIFY_MERCHANT_FEE_INFO;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("merchantNo", merchantNo);
        requestBody.addParam("channelProductCode", channelProductCode);
        requestBody.addParam("cardType", cardType);
        requestBody.addParam("bizType", bizType);
        requestBody.addParam("feeValue", feeValue);
        requestBody.addParam("capAmount", capAmount);

        return freeQuickResponse(requestUrl, requestBody);
    }

    /**
     * 商户开通产品接口：商户⼀般情况无需调用，商户注册默认会开通对应产品；代理商为子商户开通产品，通过该接口配置产品信息。
     * @param merchantNo        商户编号
     * @param channelProductCode 通道产品编码
     * @param debitRate         借记卡费率
     * @param debitCapAmount    借记卡封顶金额，不填默认999999
     * @param creditRate        贷记卡费率
     * @param creditCapAmount   贷记卡封顶金额，不填默认999999
     * @return
     */
    public static Map<String, String> registMerchantProduct(String merchantNo, String channelProductCode, String debitRate,
                                                            String debitCapAmount, String creditRate, String creditCapAmount) {
        String requestUrl = BASE_URL + REGIST_MERCHANT_PRODUCT;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("merchantNo", merchantNo);
        requestBody.addParam("channelProductCode", channelProductCode);
        requestBody.addParam("debitRate", debitRate);
        requestBody.addParam("debitCapAmount", debitCapAmount);
        requestBody.addParam("creditRate", creditRate);
        requestBody.addParam("creditCapAmount", creditCapAmount);

        return freeQuickResponse(requestUrl, requestBody);
    }

    /**
     * 商户信息查询接口
     * 商户通过该接口可以从平台通过身份证号查询用户的商户编号和秘钥。
     * @param idCardNo 身份证号码
     * @param channelProductCode 查询编码
     * @return
     */
    public static Map<String, String> queryMerchantInfo(String idCardNo, String channelProductCode) {
        String requestUrl = BASE_URL + QUERY_MERCHANT_INFO;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("idCardNo", idCardNo);
        requestBody.addParam("channelProductCode", channelProductCode);

        return freeQuickResponse(requestUrl, requestBody);
    }

    /**
     * 代付提现查询接口：商户通过该接口可以从平台查询提现结果
     * @param merchantNo 商户编号
     * @param withdrawRequestNo 提现流水号
     * @return
     */
    public static Map<String, String> queryWithDraw(String merchantNo, String withdrawRequestNo) {
        String requestUrl = BASE_URL + QUERY_WITHdRAW;

        FreeQuickRequest requestBody = freeQuickRequest();
        requestBody.addParam("merchantNo", merchantNo);
        requestBody.addParam("withdrawRequestNo", withdrawRequestNo);

        return freeQuickResponse(requestUrl, requestBody);
    }
}
